package com.invest.vo;

import com.invest.config.AppProperties;
import com.invest.infra.constants.AppConstants;
import com.invest.utils.date.DateParseHelper;
import com.invest.utils.io.FileInfo;
import com.invest.utils.io.MyFileNameUtils;
import com.invest.utils.uuid.IdWorker;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

import java.util.Date;

/**
 * <p>注释</p>
 *
 * @author yuyizhao
 * @version v0.1 2016/7/26 Exp
 */
public abstract class AbstractFileVO {

    public static final String FILE_DEFAULT_PREFIX = "f";
    public static final String FILE_PIC_PREFIX = "p";
    public static final String FILE_HTML_PREFIX = "h";

    private String today = DateParseHelper.format(new Date(),DateParseHelper.DATE_STRING_LONG);

    public String randomFileName(String filePrefix,String fileFullName){
        FileInfo fileInfo = MyFileNameUtils.getFileInfo(fileFullName);
        return new StringBuilder(filePrefix).append(IdWorker.getFileNameWork().nextId())
                .append(".").append(fileInfo.getExt()).toString();
    }

    /**
     * 文件磁盘路径
     * @param crpId
     * @param fileName
     * @param fileProperties
     * @return
     */
    public String getDiskPath(String crpId,String fileName,AppProperties fileProperties){

        return appendCommonPath(fileProperties.getStaticDiskPath(),crpId,fileName);

    }

    /**
     * 文件URI路径
     * @param crpId
     * @param fileName
     * @param fileProperties
     * @return
     */
    public String getUriPath(String crpId,String fileName,AppProperties fileProperties){
        return appendCommonPath(fileProperties.getStaticHost(),crpId,fileName);

    }

    // 企业编号 + yyyyMMddHHmmss + 文件名
    private String  appendCommonPath(String prefix,String crpId,String fileName){
        Assert.notNull(prefix,"前缀路径不能为空");
        Assert.notNull(crpId,"企业ID不能为空");
        StringBuilder builder = new StringBuilder(prefix);
        builder.append(AppConstants.SEPARATOR_PATH)
         .append(crpId)
        .append(AppConstants.SEPARATOR_PATH)
        .append(today)
        .append(AppConstants.SEPARATOR_PATH);
        if(StringUtils.isNotBlank(fileName)) {
            builder.append(fileName);
        }
        return builder.toString();
    }
}
